package com.wtwd.wtwddevice.listener.appMsg;


import com.alibaba.fastjson.JSONObject;
import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.AnswerStateEnum;
import com.wtwd.device.constant.CmdStateEnum;
import com.wtwd.device.constant.FrequencyTypeEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.model.SetFrequency;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.Frequency;
import com.wtwd.wtwddevice.mq.PushAppMsgService;
import com.wtwd.wtwddevice.service.IDeviceSendService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.IFrequencyService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@MsgType(MsgTypeEnum.APP_SET_FREQUENCY)
@Component
public class AppSetFrequencyImpl implements IMsgHandler {

    @Resource
    private IDeviceSendService deviceSendService;

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IFrequencyService frequencyService;

    @Resource
    private PushAppMsgService msgService;


    @Override
    public void handleMsg(MessageBase messageBase) {
        AppProtocol appProtocol = messageBase.getAppProtocol();
        JSONObject json = (JSONObject) appProtocol.getData();
        SetFrequency data = json.toJavaObject(SetFrequency.class);
        Integer type = data.getType();
        FrequencyTypeEnum frequencyTypeEnum = FrequencyTypeEnum.getFrequencyTypeEnum(type);
        if (frequencyTypeEnum == null) return;
        MsgTypeEnum msgTypeEnum = frequencyTypeEnum.getMsgTypeEnum();
        String number = deviceSendService.generateNumber(appProtocol.getImei());
        WTProtocol wtProtocol = WTProtocol.builder()
                .imei(appProtocol.getImei())
                .cmdName(msgTypeEnum.getCmdName())
                .body(String.valueOf(data.getValve()))
                .number(number)
                .build();
        AppProtocol protocol;
        if (deviceSendService.sendMsgDevice(wtProtocol)) {//如果为fase直接告知app端设置失败
            //插入或者更新心率频率设置值
            Long deviceId = deviceService.getDeviceIdByImei(appProtocol.getImei());
            Frequency frequency = new Frequency();
            frequency.setDeviceId(deviceId);
            frequency.setNumber(number);
            frequency.setType(frequencyTypeEnum.getType());
            frequency.setValue(data.getValve());
            frequency.setState(CmdStateEnum.SENDING.getState());
            frequencyService.insertOrUpdate(frequency);
            data.setValve(null);
            protocol = AppProtocol.builder()
                    .imei(wtProtocol.getImei())
                    .cmdName(MsgTypeEnum.APP_SET_FREQUENCY.getCmdName())
                    .state(AnswerStateEnum.NORMAL.getState())
                    .data(data)
                    .build();
        } else {
            protocol = AppProtocol.builder()
                    .imei(wtProtocol.getImei())
                    .cmdName(MsgTypeEnum.APP_SET_FREQUENCY.getCmdName())
                    .state(AnswerStateEnum.ABNORMAL.getState())
                    .data(data)
                    .build();
        }
        msgService.pushToDevice(messageBase.getForm(),
                MessageBase.builder()
                        .appProtocol(protocol)
                        .build()
        );
    }
}
